if exists (select 1
          from sysobjects
          where  id = object_id('bd_proc_reset_data')
          and type in ('P','PC'))
   drop procedure bd_proc_reset_data
go


create procedure bd_proc_reset_data (@beginDate date,@lx int,@userSerial bigint,@operator varchar(20)) as
BEGIN
	--@lx=1 调休相关（调休的申请、取消、审批通过、审批驳回，加班转调休、确认、取消、审批通过、驳回、审批通过之后按天扣除）
						 --@lx=2年假相关（年假的申请、取消、审批通过、审批驳回，法定年假的导入、录入、初始化。上年法定年假的清零
						 --         福利年假的录入、导入、清零，锁定年假的录入、导入、扣除（理解为返还、释放。和录入导入相反的操作、审批通过之后按天扣除）
-- 	bd_kq_holidays中的ho_type ：
-- 1：年假申请
--
-- 10：福利年假录入
-- 11：福利年假导入
--
-- 21：调休申请
-- 22：加班转调休申请
-- 23 : 调休录入
-- 24：调休导入
--
-- 31：锁定年假录入
-- 32：锁定年假导入
-- 33：锁定年假扣除
--
-- 41：法定年假导入
-- 42：法定年假录入
-- 43：上年法定年假清0
-- 44：法定年假初始化（1月1日系统计算（包含福利年假清零））

	--归类：
				--调休：21，22、23、24
				--年假：1,10,11,31,32,33,41,42,43,44

	--逻辑描述：重新计算从数据收集分为调休、年假两类，以@beginDate为时间节点，重新计算包含@beginDate和其之后的相关记录
						--收集重新计算初始值（不同的ho_type，条件不同）

	DECLARE @hoId bigint --可用调休
	DECLARE @hoDate varchar(10) --可用调休
	DECLARE @hoType int --剩余调休
	DECLARE @useTime int = 0 --剩余调休
	DECLARE @prevLegalBefore int = 0 --扣除前上年法定
	DECLARE @curLegalBefore int = 0 --扣除前当年法定
	DECLARE @curWelfareBefore int = 0 --扣除前当年福利
	DECLARE @curCompBefore int = 0 --扣除前调休
	DECLARE @prevLegalUsable int = 0 --可用上年法定
	DECLARE @curLegalUsable int = 0 --可用当年法定
	DECLARE @curWelfareUsable int = 0 --可用当年福利
	DECLARE @curCompUsable int = 0 --可用调休（其实里边包含未处理的加班转调休数据，申请加班转调休时，以此为准）
	DECLARE @prevLegalRemain int = 0 --剩余上年法定
	DECLARE @curLegalRemain int = 0 --剩余当年法定
	DECLARE @curWelfareRemain int = 0 --剩余当年福利
	DECLARE @curCompRemain int = 0 --剩余调休（实时剩余）
	DECLARE @dataStatus int --处理状态
	DECLARE @relatedId int--单子ID(仅请假、加班)

	DECLARE @txsx int --调休上限
	select @txsx = txsx from bd_nianjia where user_serial = @userSerial;

	DECLARE @counts int = 0 --多用途，返回查询的数据条数
	DECLARE @flag int = 0 --多用途，返回查询的数据字段值
	DECLARE @status int = 0 --多用途，返回查询的数据字段值
	DECLARE @isdel int = 0 --单子是否已取消
	DECLARE @whetherValidity int = 0 --明细中记录是否无效化

	DECLARE @curDate varchar(10) = convert(varchar(10),getdate(),120) --执行日期



	DECLARE @tempUseTime int=0 --临时时间量


	DECLARE @fdClearDay varchar(10) --上年法定年假失效时间

	select top 1 @fdClearDay = SUBSTRING(convert(varchar(10),fd_nj_clear_date,120), 6, 5) from wt_reg;


-- 	DECLARE @initCurCompUsable int = 0 --初始化调休（作为重新计算扣除前）
-- 	DECLARE @initCurCompRemain int = 0 --初始化剩余调休（作为重新计算扣除前）


	--@lx=1 调休相关数据重新计算
	if @lx=1
		BEGIN
			--调休，需要的数据有：可用调休，剩余调休
			--1.收集重新计算初始值

			--取@beginDate之前的、并且类型是21，22的最近的一条记录中的字段值作为初始值
			select top 1 @curCompBefore = cur_comp_usable ,@curCompRemain = cur_comp_remain
			from bd_kq_holidays
			where user_serial = @userSerial and ho_date < @beginDate and ho_type in (21,22,23,24) and data_status !=1

			order by ho_date desc,ho_type,create_time desc

			--2.根据@beginDate和其之后的相关记录创建游标，

			DECLARE resetRecord cursor LOCAL for
			select id,convert(varchar(10),ho_date,120),ho_type,use_time,data_status,related_id
			from bd_kq_holidays
			where user_serial = @userSerial and ho_date >= @beginDate and ho_type in (21,22,23,24) and data_status !=1
			order by ho_date ,ho_type desc,create_time

			open resetRecord

			fetch next from resetRecord into @hoId,@hoDate,@hoType,@useTime,@dataStatus,@relatedId

			while @@FETCH_STATUS=0
				begin
					set @counts = 0;
					set @flag = 0;
					set @dataStatus = 0;
					set @isdel = 0;
					set @whetherValidity = 0
					set @status = 0
					set @tempUseTime = 0;

					--逐条处理，@hoType = 21 ：调休请假记录，初始值减去@useTime，若该单子已经审批,则<=@beginDate的记录需要计算剩余调休
										--@hoType = 22 : 加班转调休，初始值加上@useTime，若该单子已经确认工时,则<=@beginDate的记录需要计算剩余调休
					if @hoType = 21
						BEGIN
							----判断单子是否已审批、是否已取消、是否已无效化
							----若已取消（取消单子）或无效化（该人在单子中的记录）就仅仅更改状态，
							----反之，除了状态之外还要处理假期数据。
							--1、根据加班单的id,获取状态
							select @status = o.status,@isdel = o.is_del,@whetherValidity = k.whether_validity
							from bd_leave_order o
							INNER JOIN lr_kqingj k on o.order_id = k.order_id
							where convert(varchar(10),k.kssj,120) = @hoDate
										and k.order_id = @relatedId
										and k.user_serial = @userSerial;
							--2、不通的状态执行逻辑
							--分两大类：1未审批和已审批通过，统称为有效。2已审批驳回、取消、无效化，统称为无效
							if @status != 2 and @isdel = 0 and 	@whetherValidity = 1
								BEGIN
									set @curCompUsable = @curCompBefore - @useTime;

									if @status in (3,4) and @hoDate <= @curDate
										BEGIN
											set @curCompRemain = @curCompRemain-@useTime;
											set @dataStatus = 2
										END
									--更新该记录
									update bd_kq_holidays
										set cur_comp_before = @curCompBefore,
												cur_comp_usable = @curCompUsable,
												cur_comp_remain = @curCompRemain,
												data_status = @dataStatus,
												update_by = @operator,
												update_time = GETDATE()
										where id = @hoId;

									--将本记录的可用调休，作为下一记录的扣除前调休
									set @curCompBefore = @curCompUsable

								END
							else
								BEGIN
									--单子已取消
									set @dataStatus = 1
									update bd_kq_holidays
										set data_status = @dataStatus,
												update_by = @operator,
												update_time = GETDATE()
										where id = @hoId;
								END
						END
					if @hoType = 22
						BEGIN
							--加班转调休，先计算到剩余调休，然后根据日期和是否确认加班来确定是否需要计算可用
							----判断单子是否已审批、是否已取消、是否已无效化
							----若已取消（取消单子）或无效化（该人在单子中的记录）就仅仅更改状态，
							----反之，除了状态之外还要处理假期数据。
							--1、根据加班单的id,获取状态
							select @status = o.status,@isdel = o.is_delete,@whetherValidity = j.is_delete
							from bd_over_time_order o
							INNER JOIN lr_jiaba j on o.order_id = j.order_id
							where convert(varchar(10),j.jbrq,120) = @hoDate
										and j.order_id = @relatedId
										and j.user_serial = @userSerial;
							--2、不通的状态执行逻辑
							--分两大类：1未审批和已审批通过，统称为有效。2已审批驳回、取消、无效化，统称为无效
							if @status != 2 and @isdel = 0 and 	@whetherValidity = 0
								BEGIN

									--判断单子是否已确认加班工时
									select @tempUseTime = isnull(ot_to_rest_hours,0),@flag =  if_operation
									from  bd_kq_timecard
									where  isnull(ot_to_rest_hours,0) > 0 and user_serial = @userSerial and card_date = @hoDate
									--未确认
									if @flag = 0
										BEGIN
											set @curCompUsable = @curCompBefore
											if (@curCompRemain + @useTime) > @txsx
												BEGIN
													set @curCompRemain = @txsx
												END
											ELSE
												BEGIN
													set @curCompRemain = @curCompRemain + @useTime;
												END
										END
									else
										BEGIN
											set @useTime = @tempUseTime;
											if (@curCompRemain + @useTime) > @txsx
												BEGIN
													set @curCompRemain = @txsx
												END
											ELSE
												BEGIN
													set @curCompRemain = @curCompRemain + @useTime;
												END

											if (@curCompBefore + @useTime) > @txsx
												BEGIN
													set @curCompUsable = @txsx
												END
											ELSE
												BEGIN
													set @curCompUsable = @curCompBefore + @useTime
												END
											set @dataStatus = 2
										END

									--更新该记录
									update bd_kq_holidays
										set cur_comp_before = @curCompBefore,
												cur_comp_usable = @curCompUsable,
												cur_comp_remain = @curCompRemain,
												use_time = @useTime,
												data_status = @dataStatus,
												update_by = @operator,
												update_time = GETDATE()
										where id = @hoId;

									--将本记录的可用调休，作为下一记录的扣除前调休
									set @curCompBefore = @curCompUsable
								END
							else
								BEGIN
									--该记录无效
									set @dataStatus = 1;
									update bd_kq_holidays
										set data_status = @dataStatus,
												update_by = @operator,
												update_time = GETDATE()
										where id = @hoId;
								END
						END
					--3.调休录入、导入
					--直接加值
					if @hoType in (23,24)
						BEGIN
							set @curCompUsable = @curCompBefore
						--先计算到剩余调休，然后根据日期和是否确认加班来确定是否需要计算可用
							if (@curCompRemain + @useTime) > @txsx
								BEGIN
									set @curCompRemain = @txsx
								END
							ELSE
								BEGIN
									set @curCompRemain = @curCompRemain + @useTime;
								END

							if (@curCompBefore + @useTime) > @txsx
								BEGIN
									set @curCompUsable = @txsx
								END
							ELSE
								BEGIN
									set @curCompUsable = @curCompBefore + @useTime
								END
							set @dataStatus = 2
							--更新该记录
							update bd_kq_holidays
								set cur_comp_before = @curCompBefore,
										cur_comp_usable = @curCompUsable,
										cur_comp_remain = @curCompRemain,
										data_status = @dataStatus,
										update_by = @operator,
										update_time = GETDATE()
								where id = @hoId;

							--将本记录的可用调休，作为下一记录的扣除前调休
							set @curCompBefore = @curCompUsable
						END
					fetch next from resetRecord into @hoId,@hoDate,@hoType,@useTime,@dataStatus,@relatedId
				END

		--逐条处理之后，整理假期额度表数据，用于更新
		--更新字段，可用调休（对应@curCompUsable）、按生效剩余调休（对应@curCompRemain）
	  --DECLARE @sdtx int = 0 --冻结的调休

			--更新假期账户表
			update bd_nianjia
				set txts = @curCompUsable,
						real_txts = @curCompRemain
				where user_serial = @userSerial

		END

	--@lx=2 年假相关数据重新计算
	if @lx=2
		BEGIN
			--调休，需要的数据有：可用调休，剩余调休
			--1.收集重新计算初始值

			--取@beginDate之前的、并且类型是21，22的最近的一条记录中的字段值作为初始值
			select top 1
				@prevLegalBefore = prev_legal_usable,
				@curLegalBefore = cur_legal_usable,
				@curWelfareBefore = cur_welfare_usable,
				@prevLegalRemain = prev_legal_remain,
				@curLegalRemain= cur_legal_remain,
				@curWelfareRemain = cur_welfare_remain
			from bd_kq_holidays
			where user_serial = @userSerial and ho_date < @beginDate and ho_type in (1,10,11,31,32,33,41,42,43,44) and data_status !=1

			order by ho_date desc,ho_type,create_time desc

			--2.根据@beginDate和其之后的相关记录创建游标，

			DECLARE resetRecord cursor LOCAL for
			select id,ho_date,ho_type,use_time,data_status,related_id
			from bd_kq_holidays
			where user_serial = @userSerial and ho_date >= @beginDate and ho_type in (1,10,11,31,32,33,41,42,43,44) and data_status !=1
			order by ho_date ,ho_type desc,create_time

			open resetRecord

			fetch next from resetRecord into @hoId,@hoDate,@hoType,@useTime,@dataStatus,@relatedId

			while @@FETCH_STATUS=0
				begin
					set @counts = 0;
					set @flag = 0;
					set @dataStatus = 0;
					set @isdel = 0;
					set @whetherValidity = 0
					set @status = 0
					set @tempUseTime = 0;

					--逐条处理，@hoType = 44 ：法定年假初始化（1月1日系统计算（包含福利年假清零）），处理：仅仅更新上年年假
										--@hoType = 43 : 上年法定年假清0
										--@hoType in (42,41) : 法定年假录入、导入
										--@hoType in (33,32,31) : 锁定年假录入、导入、扣除
										--@hoType in (10,11) : 福利年假录入、导入
										--@hoType = 1 : 年假申请

					--@hoType = 44 ：法定年假初始化（1月1日系统计算（包含福利年假清零）），处理：仅仅更新上年年假
					if @hoType = 44
						BEGIN

							--更新上年年假值为当前当年年假的值
							--更新福利年假 为0
							--更新当前年假值为use_time的值。
							set @prevLegalUsable = @curLegalBefore;
							set @curLegalUsable = @useTime;
							set @curWelfareUsable = 0;
							--同时更新remain为后缀的年假字段同当年可用
							if @hoDate <= @curDate
								BEGIN
									set @prevLegalRemain = @prevLegalUsable;
									set @curLegalRemain = @curLegalUsable;
									set @curWelfareRemain = @curWelfareUsable;
									set @dataStatus = 2
								END
							--更新该记录
							update bd_kq_holidays
								set prev_legal_before = @prevLegalBefore,
										cur_legal_before = @curLegalBefore,
										cur_welfare_before = @curWelfareBefore,
										prev_legal_usable = @prevLegalUsable,
										cur_legal_usable = @curLegalUsable,
										cur_welfare_usable = @curWelfareUsable,
										prev_legal_remain = @prevLegalRemain,
										cur_legal_remain = @curLegalRemain,
										cur_welfare_remain = @curWelfareRemain,
										data_status = @dataStatus,
										update_by = @operator,
										update_time = GETDATE()
								where id = @hoId;

							--将本记录的可用年假信息，作为下一记录的扣除前年假信息
							set @prevLegalBefore = @prevLegalUsable;
							set @curLegalBefore = @curLegalUsable;
							set @curWelfareBefore = @curWelfareUsable;

						END

					--@hoType = 43 : 上年法定年假清0
					if @hoType = 43
						BEGIN
							--
							set @useTime = @prevLegalBefore;
							set @prevLegalUsable = 0;
							set @curLegalUsable = @curLegalBefore;
							set @curWelfareUsable = @curWelfareBefore;

							if @hoDate <= @curDate
								BEGIN
									set @prevLegalRemain = 0;
									set @dataStatus = 2
								END
							--更新该记录
							update bd_kq_holidays
								set prev_legal_before = @prevLegalBefore,
										cur_legal_before = @curLegalBefore,
										cur_welfare_before = @curWelfareBefore,
										prev_legal_usable = @prevLegalUsable,
										cur_legal_usable = @curLegalUsable,
										cur_welfare_usable = @curWelfareUsable,
										prev_legal_remain = @prevLegalRemain,
										cur_legal_remain = @curLegalRemain,
										cur_welfare_remain = @curWelfareRemain,
										use_time = @useTime,
										data_status = @dataStatus,
										update_by = @operator,
										update_time = GETDATE()
								where id = @hoId;

							--将本记录的可用年假信息，作为下一记录的扣除前年假信息
							set @prevLegalBefore = @prevLegalUsable;
							set @curLegalBefore = @curLegalUsable;
							set @curWelfareBefore = @curWelfareUsable;

						END

					--@hoType in (42,41) : 法定年假录入、导入
					if @hoType in (42,41)
						BEGIN
							--更新当年法定年假值
							set @prevLegalUsable = @prevLegalBefore;
							set @curLegalUsable = @curLegalBefore + @useTime;
							set @curWelfareUsable = @curWelfareBefore;
							--同时更新remain为后缀的年假字段同当年可用
							if @hoDate <= @curDate
								BEGIN
									set @curLegalRemain = @curLegalRemain+ @useTime;
									set @dataStatus = 2
								END
							--更新该记录
							update bd_kq_holidays
								set prev_legal_before = @prevLegalBefore,
										cur_legal_before = @curLegalBefore,
										cur_welfare_before = @curWelfareBefore,
										prev_legal_usable = @prevLegalUsable,
										cur_legal_usable = @curLegalUsable,
										cur_welfare_usable = @curWelfareUsable,
										prev_legal_remain = @prevLegalRemain,
										cur_legal_remain = @curLegalRemain,
										cur_welfare_remain = @curWelfareRemain,
										data_status = @dataStatus,
										update_by = @operator,
										update_time = GETDATE()
								where id = @hoId;

							--将本记录的可用年假信息，作为下一记录的扣除前年假信息
							set @prevLegalBefore = @prevLegalUsable;
							set @curLegalBefore = @curLegalUsable;
							set @curWelfareBefore = @curWelfareUsable;

						END

					--@hoType in (33,32,31) : 锁定年假录入、导入、扣除
					if @hoType in (33,32,31)
						BEGIN
							--更新当年法定年假值
							set @prevLegalUsable = @prevLegalBefore;
							set @curLegalUsable = @curLegalBefore - @useTime;
							set @curWelfareUsable = @curWelfareBefore;
							--同时更新remain为后缀的年假字段同当年可用
							if @hoDate <= @curDate
								BEGIN
									set @curLegalRemain = @curLegalRemain - @useTime;
									set @dataStatus = 2
								END
							--更新该记录
							update bd_kq_holidays
								set prev_legal_before = @prevLegalBefore,
										cur_legal_before = @curLegalBefore,
										cur_welfare_before = @curWelfareBefore,
										prev_legal_usable = @prevLegalUsable,
										cur_legal_usable = @curLegalUsable,
										cur_welfare_usable = @curWelfareUsable,
										prev_legal_remain = @prevLegalRemain,
										cur_legal_remain = @curLegalRemain,
										cur_welfare_remain = @curWelfareRemain,
										data_status = @dataStatus,
										update_by = @operator,
										update_time = GETDATE()
								where id = @hoId;

							--将本记录的可用年假信息，作为下一记录的扣除前年假信息
							set @prevLegalBefore = @prevLegalUsable;
							set @curLegalBefore = @curLegalUsable;
							set @curWelfareBefore = @curWelfareUsable;
						END

					--@hoType in (10,11) : 福利年假录入、导入
					if @hoType in (10,11)
						BEGIN
							--更新当年福利年假值
							set @prevLegalUsable = @prevLegalBefore;
							set @curLegalUsable = @curLegalBefore;
							set @curWelfareUsable = @curWelfareBefore + @useTime;
							--同时更新remain为后缀的年假字段同当年可用
							if @hoDate <= @curDate
								BEGIN
									set @curWelfareRemain = @curWelfareRemain + @useTime;
									set @dataStatus = 2
								END
							--更新该记录
							update bd_kq_holidays
								set prev_legal_before = @prevLegalBefore,
										cur_legal_before = @curLegalBefore,
										cur_welfare_before = @curWelfareBefore,
										prev_legal_usable = @prevLegalUsable,
										cur_legal_usable = @curLegalUsable,
										cur_welfare_usable = @curWelfareUsable,
										prev_legal_remain = @prevLegalRemain,
										cur_legal_remain = @curLegalRemain,
										cur_welfare_remain = @curWelfareRemain,
										data_status = @dataStatus,
										update_by = @operator,
										update_time = GETDATE()
								where id = @hoId;

							--将本记录的可用年假信息，作为下一记录的扣除前年假信息
							set @prevLegalBefore = @prevLegalUsable;
							set @curLegalBefore = @curLegalUsable;
							set @curWelfareBefore = @curWelfareUsable;
						END

					--@hoType = 1 : 年假申请
					if @hoType = 1
						BEGIN
							--年假使用
							--以请假当天的情况来定，关键点：申请时当年3月1日
							--1、根据加班单的id,获取状态
							select @status = o.status,@isdel = o.is_del,@whetherValidity = k.whether_validity
							from bd_leave_order o
							INNER JOIN lr_kqingj k on o.order_id = k.order_id
							where convert(varchar(10),k.kssj,120) = @hoDate
										and k.order_id = @relatedId
										and k.user_serial = @userSerial;
							--2、不通的状态执行逻辑
							--分两大类：1未审批和已审批通过，统称为有效。2已审批驳回、取消、无效化，统称为无效
							set @tempUseTime = @useTime;
							if @status != 2 and @isdel = 0 and 	@whetherValidity = 1
								BEGIN
									--1月1日-3月1日：按照去年法定--今年法定--今年福利的扣除顺序（包含1月1日，不包含3月1日）
									--之外：按照今年法定--今年福利扣除（不论是今年还是往年，都根据请假执行日期为参考点）
									if SUBSTRING(@hoDate, 6, 5)< '03-01' and SUBSTRING(@hoDate, 6, 5)>='01-01'
										BEGIN
											--去年法定--今年法定--今年福利
											if @prevLegalBefore>=@tempUseTime
												BEGIN
													set @prevLegalUsable = @prevLegalBefore - @tempUseTime
												  set @curLegalUsable = @curLegalBefore
													set @curWelfareUsable = @curWelfareBefore
												END
											else
												BEGIN
													set @prevLegalUsable = 0;
													--剩余部分
													set @tempUseTime = @tempUseTime - @prevLegalBefore
													if @curLegalBefore>= @tempUseTime
														BEGIN
															set @curLegalUsable = @curLegalBefore-@tempUseTime
															set @curWelfareUsable = @curWelfareBefore
														END
													else
														BEGIN
															set @curLegalUsable = 0
															set @tempUseTime = @tempUseTime - @curLegalBefore
															set @curWelfareUsable = @curWelfareBefore - @tempUseTime
														END
												END
										END
									ELSE
										BEGIN
											--今年法定--今年福利
											set @prevLegalUsable = @prevLegalBefore
											if @curLegalBefore>= @tempUseTime
												BEGIN
													set @curLegalUsable = @curLegalBefore-@tempUseTime
													set @curWelfareUsable = @curWelfareBefore
												END
											else
												BEGIN
													set @curLegalUsable = 0
													set @tempUseTime = @tempUseTime - @curLegalBefore
													set @curWelfareUsable = @curWelfareBefore - @tempUseTime
												END
										END

									if @status in (3,4) and @hoDate <= @curDate
										BEGIN
											--1月1日-3月1日：按照去年法定--今年法定--今年福利的扣除顺序（包含1月1日，不包含3月1日）
											--之外：按照今年法定--今年福利扣除（不论是今年还是往年，都根据请假执行日期为参考点）
											if SUBSTRING(@hoDate, 6, 5)< @fdClearDay and SUBSTRING(@hoDate, 6, 5)>='01-01'
												BEGIN
													--去年法定--今年法定--今年福利
													if @prevLegalRemain>=@tempUseTime
														BEGIN
															set @prevLegalRemain = @prevLegalRemain - @tempUseTime
														END
													else
														BEGIN
															set @prevLegalRemain = 0;
															--剩余部分
															set @tempUseTime = @tempUseTime - @prevLegalRemain
															if @curLegalRemain>= @tempUseTime
																BEGIN
																	set @curLegalRemain = @curLegalRemain-@tempUseTime
																END
															else
																BEGIN
																	set @curLegalRemain = 0
																	set @tempUseTime = @tempUseTime - @curLegalRemain
																	set @curWelfareRemain = @curWelfareRemain - @tempUseTime
																END
														END
												END
											ELSE
												BEGIN
													--今年法定--今年福利
													--剩余部分
													if @curLegalRemain>= @tempUseTime
														BEGIN
															set @curLegalRemain = @curLegalRemain-@tempUseTime
														END
													else
														BEGIN
															set @curLegalRemain = 0
															set @tempUseTime = @tempUseTime - @curLegalRemain
															set @curWelfareRemain = @curWelfareRemain - @tempUseTime
														END
												END
											set @dataStatus = 2
										END
									--更新该记录
									update bd_kq_holidays
										set prev_legal_before = @prevLegalBefore,
												cur_legal_before = @curLegalBefore,
												cur_welfare_before = @curWelfareBefore,
												prev_legal_usable = @prevLegalUsable,
												cur_legal_usable = @curLegalUsable,
												cur_welfare_usable = @curWelfareUsable,
												prev_legal_remain = @prevLegalRemain,
												cur_legal_remain = @curLegalRemain,
												cur_welfare_remain = @curWelfareRemain,
												data_status = @dataStatus,
												update_by = @operator,
												update_time = GETDATE()
										where id = @hoId;

									--将本记录的可用年假信息，作为下一记录的扣除前年假信息
									set @prevLegalBefore = @prevLegalUsable;
									set @curLegalBefore = @curLegalUsable;
									set @curWelfareBefore = @curWelfareUsable;
								END
							else
								BEGIN
									--单子已取消
									set @dataStatus = 1
									update bd_kq_holidays
										set data_status = @dataStatus,
												update_by = @operator,
												update_time = GETDATE()
										where id = @hoId;
								END
						END

					fetch next from resetRecord into @hoId,@hoDate,@hoType,@useTime,@dataStatus,@relatedId
				END
			--逐条处理之后，整理假期额度表数据，用于更新
			--更新字段，可用调休（对应@curCompUsable）、按生效剩余调休（对应@curCompRemain）
			--DECLARE @sdtx int = 0 --冻结的调休

			--更新假期账户表
			update bd_nianjia 
				set lastfdnj = @prevLegalBefore,
						fdnjts = @curLegalBefore,
						flnjts = @curWelfareBefore
				where user_serial = @userSerial
		END
END
go
